package com.atguigu.day07;

import com.atguigu.bean.Event;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class Flink03_SplitStreamByOutput {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.从自定义数据源获取数据
        DataStreamSource<Event> streamSource = env.addSource(new Flink02_SplitStreamByFilter.ClickSource());

        //3.通过侧输出流将不同用的数据放到不同的流里面
        SingleOutputStreamOperator<String> result = streamSource.process(new ProcessFunction<Event, String>() {
            @Override
            public void processElement(Event value, Context ctx, Collector<String> out) throws Exception {
                if ("Songsong".equals(value.user)) {
                    ctx.output(new OutputTag<String>("song") {
                    }, value.toString());
                } else if ("Bingbing".equals(value.user)) {
                    ctx.output(new OutputTag<String>("bing") {
                    }, value.toString());
                } else {
                    out.collect(value.toString());
                }
            }
        });

        result.print("other");
        //获取到songsong侧流的数据
        result.getSideOutput(new OutputTag<String>("song") {
        }).print("song");
        //获取到bingbing侧流的数据
        result.getSideOutput(new OutputTag<String>("bing") {
        }).print("bing");

        env.execute();
    }
}
